﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrystalDecisions.CrystalReports.Engine;

namespace common
{
    public static class reportLibs
    {
        public static myTypes.PrintLayout GetLayout(ReportClass report)
        {
            myTypes.PrintLayout layout = new myTypes.PrintLayout();
            try
            {
                //PaperSize
                switch (report.PrintOptions.PaperSize)
                {
                    case CrystalDecisions.Shared.PaperSize.PaperA4:
                        layout.PaperSize = PaperSizeEnum.PaperA4;
                        break;
                    case CrystalDecisions.Shared.PaperSize.PaperA4Small:
                        layout.PaperSize = PaperSizeEnum.PaperA4Small;
                        break;
                    case CrystalDecisions.Shared.PaperSize.PaperLetter:
                        layout.PaperSize = PaperSizeEnum.PaperLetter;
                        break;
                    case CrystalDecisions.Shared.PaperSize.PaperLetterSmall:
                        layout.PaperSize = PaperSizeEnum.PaperLetterSmall;
                        break;
                    case CrystalDecisions.Shared.PaperSize.PaperLegal:
                        layout.PaperSize = PaperSizeEnum.PaperLegal;
                        break;
                    case CrystalDecisions.Shared.PaperSize.PaperA5:
                        layout.PaperSize = PaperSizeEnum.PaperA5;
                        break;
                }
                //PaperOrientation
                switch (report.PrintOptions.PaperOrientation)
                {
                    case CrystalDecisions.Shared.PaperOrientation.Portrait:
                        layout.PaperOrientation = PaperOrientationEnum.Portrait;
                        break;
                    case CrystalDecisions.Shared.PaperOrientation.Landscape:
                        layout.PaperOrientation = PaperOrientationEnum.Landscape;
                        break;
                }
                //Margin
                int dpiX = 0, dpiY = 0;
                GetDPI(report, out dpiX, out dpiY);
                layout.useCustomMargin = false;
                layout.leftMargin = (decimal)report.PrintOptions.PageMargins.leftMargin / dpiX;
                layout.rightMargin = (decimal)report.PrintOptions.PageMargins.rightMargin / dpiX;
                layout.topMargin = (decimal)report.PrintOptions.PageMargins.topMargin / dpiY;
                layout.bottomMargin = (decimal)report.PrintOptions.PageMargins.bottomMargin / dpiY;
            }
            catch { }
            return layout;
        }
        public static void SetLayout(ReportClass report, myTypes.PrintLayout value, myTypes.PrintLayout defaultValue)
        {
            try
            {
                if (value == null) return;
                //Unknown behavior in CR : we must CHANGE to PaperSize to make the margin effect immediately
                report.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.DefaultPaperSize;

                //PaperSize
                PaperSizeEnum paperSize = (defaultValue != null && value.PaperSize == PaperSizeEnum.PaperDefault ? defaultValue.PaperSize : value.PaperSize);
                switch (paperSize)
                {
                    case PaperSizeEnum.PaperA4:
                        report.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4;
                        break;
                    case PaperSizeEnum.PaperA4Small:
                        report.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4Small;
                        break;
                    case PaperSizeEnum.PaperLetter:
                        report.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperLetter;
                        break;
                    case PaperSizeEnum.PaperLetterSmall:
                        report.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperLetterSmall;
                        break;
                    case PaperSizeEnum.PaperLegal:
                        report.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperLegal;
                        break;
                    case PaperSizeEnum.PaperA5:
                        report.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA5;
                        break;

                }
                //PaperOrientation
                PaperOrientationEnum paperOrientation = (defaultValue != null && value.PaperOrientation == PaperOrientationEnum.Default ? defaultValue.PaperOrientation : value.PaperOrientation);
                switch (paperOrientation)
                {
                    case PaperOrientationEnum.Portrait:
                        report.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait;
                        break;
                    case PaperOrientationEnum.Landscape:
                        report.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;
                        break;
                }
                //Margin
                int dpiX = 0, dpiY = 0;
                GetDPI(report, out dpiX, out dpiY);
                CrystalDecisions.Shared.PageMargins margin = new CrystalDecisions.Shared.PageMargins();
                if (defaultValue != null && !value.useCustomMargin)
                {
                    margin.leftMargin = (int)(defaultValue.leftMargin * dpiX);
                    margin.rightMargin = (int)(defaultValue.rightMargin * dpiX);

                    margin.topMargin = (int)(defaultValue.topMargin * dpiY);
                    margin.bottomMargin = (int)(defaultValue.bottomMargin * dpiY);
                }
                else
                {
                    margin.leftMargin = (int)(value.leftMargin * dpiX);
                    margin.rightMargin = (int)(value.rightMargin * dpiX);

                    margin.topMargin = (int)(value.topMargin * dpiY);
                    margin.bottomMargin = (int)(value.bottomMargin * dpiY);
                }
                report.PrintOptions.ApplyPageMargins(margin);
            }
            catch (Exception er)
            {
                errorHandler.lastException = er;
            }
        }
        public static void GetDPI(ReportClass report, out int x, out int y)
        {
            System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
            if (report.PrintOptions.PrinterName != null && report.PrintOptions.PrinterName.Trim() != "")
                ps.PrinterName = report.PrintOptions.PrinterName;
            else //Get default printer 
            {
                System.Drawing.Printing.PrinterSettings settings = new System.Drawing.Printing.PrinterSettings();
                ps.PrinterName = settings.PrinterName;
            }
            x = ps.DefaultPageSettings.PrinterResolution.X;
            y = ps.DefaultPageSettings.PrinterResolution.Y;
        }

        public static System.Drawing.Printing.PrinterSettings GetPrinterSettings(ReportClass report)
        {
            System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
            if (report.PrintOptions.PrinterName != null && report.PrintOptions.PrinterName.Trim() != "")
                ps.PrinterName = report.PrintOptions.PrinterName;
            else //Get default printer 
            {
                System.Drawing.Printing.PrinterSettings settings = new System.Drawing.Printing.PrinterSettings();
                ps.PrinterName = settings.PrinterName;
                
            }
            return ps;
        }

        public static decimal Test()
        {
            decimal val = 0;
            System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
            ps.PrinterName = "PDFCreator";
            return val;
        }

    }
}
